package com.iRomantic.domain.entity;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.coobird.thumbnailator.Thumbnails;

import com.iRomantic.common.oss.OSSService;
import com.iRomantic.common.util.FileUtil;
import com.iRomantic.config.Constant;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.upload.UploadFile;

/**
 * 图片
 * @author serenity
 *
 */
public class Picture extends Model<Picture> {

	private static final long serialVersionUID = 5044242897215166510L;
	
	private static final Logger logger = LoggerFactory.getLogger(Picture.class);
	
	public static final Picture DAO = new Picture();
	
	/**
	 * 普通图片
	 */
	public static final int TYPE_NORMAL = 0;
	
	/**
	 * 精选图片
	 */
	public static final int TYPE_MAIN = 1;
	
	
	
	public boolean save(){
		this.set("id", UUID.randomUUID().toString());
		this.set("createTime",new Timestamp(System.currentTimeMillis()));
		return super.save();
	}
	
	/**
	 * 根据上传文件初始化属性
	 * @param file
	 * @param savePath
	 * @throws Exception 
	 */
	public void initAttrByUpload(UploadFile file,String savePath) throws Exception{
		
		//保存图片
		String pictureName = FileUtil.randomFileName(file.getFileName());
		//file.getFile().renameTo(new File(savePath,pictureName));
		BufferedImage img = ImageIO.read(file.getFile());
		int width = img.getWidth();
		int height = img.getHeight();
		
		//生成缩略图
		BufferedImage thumbnail = Thumbnails.of(file.getFile()).width(300).asBufferedImage();
		String thumbnailsName = "thumbnail_"+pictureName;
		File thumbnailsFile = new File(savePath,thumbnailsName);
		int thumbnailWidth = thumbnail.getWidth();
		int thumbnailHeight = thumbnail.getHeight();
		ImageIO.write(thumbnail, "jpg", thumbnailsFile);
		
		//设置属性
		set("url", pictureName);
		set("width", width);
		set("height",height);
		set("thumbnailUrl", thumbnailsName);
		set("thumbnailHeight", thumbnailHeight);
		set("thumbnailWidth", thumbnailWidth);
		 
		
		
		//上传云存储
		OSSService.getInstance().save(Constant.PICTURE_PICTRUE+this.getStr("weddingId")+"/"+pictureName, file.getFile());
		OSSService.getInstance().save(Constant.PICTURE_PICTRUE+this.getStr("weddingId")+"/"+thumbnailsName, thumbnailsFile);
		file.getFile().delete();
		thumbnailsFile.delete();
	}
	
	/**
	 * 根据婚礼ID和图片类型查询
	 * @param wedding
	 * @param type
	 * @return
	 */
	public List<Picture> findByWeddingAndType(String weddingId,int type){
		return Picture.DAO.find("select * from t_picture where weddingId=? and type=?", weddingId, type);
	}
	
	/**
	 * 根据婚礼ID查询
	 * @param weddingId
	 * @return
	 */
	public List<Picture> findByWedding(String weddingId){
		return Picture.DAO.find("select * from t_picture where weddingId=? ", weddingId);
	}
	
	/**
	 * 设置资源全路径
	 * @param list
	 */
	public void setOSSPath(List<Picture> list){
		for(Picture p : list){
			p.set("url", Constant.OSS_PATH+Constant.PICTURE_PICTRUE +p.getStr("weddingId")+"/"+p.get("url"));
			p.set("thumbnailUrl", Constant.OSS_PATH+Constant.PICTURE_PICTRUE+p.getStr("weddingId")+"/" +p.get("thumbnailUrl"));
		}
	}
	
	

}
